package me.shibor.study.servlet.file;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet("/uploadFile")
@MultipartConfig
public class UploadFileServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		Part part = req.getPart("fileName");
		
		//获取请求信息
		String name = part.getHeader("content-disposition");
		System.out.println(name);
		
		String realPath = req.getServletContext().getRealPath("/temp");
		System.out.println("realPath: " + realPath);
		
		String fileType = name.substring(name.lastIndexOf("."), name.length()-1);
		
		String CopyFile = realPath + "/" + UUID.randomUUID() + fileType;
		
		part.write(CopyFile);
	}

}
